/* 
 * Copyright (C) 2018 Igor Maznitsa.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package com.igormaznitsa.sciareto.ui.editors.mmeditors;

import com.igormaznitsa.mindmap.model.logger.Logger;
import com.igormaznitsa.mindmap.model.logger.LoggerFactory;
import com.igormaznitsa.sciareto.ui.UiUtils;
import java.awt.Toolkit;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.ImageIcon;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import com.igormaznitsa.mindmap.ide.commons.SwingUtils;
import com.igormaznitsa.mindmap.swing.panel.utils.Utils;
import com.igormaznitsa.mindmap.swing.services.UIComponentFactoryProvider;
import com.igormaznitsa.mindmap.swing.panel.utils.Focuser;

public final class UriEditPanel extends javax.swing.JPanel  {

  private static final long serialVersionUID = -6683682013891751388L;

  private static final Logger LOGGER = LoggerFactory.getLogger(UriEditPanel.class);

  private static final ImageIcon IMAGE_OK = new ImageIcon(UiUtils.loadIcon("tick16.png")); //NOI18N
  private static final ImageIcon IMAGE_BAD = new ImageIcon(UiUtils.loadIcon("cancel.png")); //NOI18N
  private static final ImageIcon IMAGE_QUESTION = new ImageIcon(UiUtils.loadIcon("question16.png")); //NOI18N

  @SuppressWarnings("ResultOfObjectAllocationIgnored")
  public UriEditPanel(final String uri) {
    initComponents();

    this.textFieldURI.setText(uri == null ? "" : uri); //NOI18N
    this.textFieldURI.setComponentPopupMenu(SwingUtils.addTextActions(UIComponentFactoryProvider.findInstance().makePopupMenu()));
    
    this.textFieldURI.getDocument().addDocumentListener(new DocumentListener() {

      @Override
      public void insertUpdate(DocumentEvent e) {
        validateUri();
      }

      @Override
      public void removeUpdate(DocumentEvent e) {
        validateUri();
      }

      @Override
      public void changedUpdate(DocumentEvent e) {
        validateUri();
      }
    });

    new Focuser(this.textFieldURI);
    
    validateUri();
  }

  public String getText() {
    return this.textFieldURI.getText().trim();
  }

  private void validateUri() {
    final String text = this.textFieldURI.getText().trim();
    this.labelValidator.setText(""); //NOI18N
    if (text.isEmpty()) {
      this.labelValidator.setIcon(IMAGE_QUESTION);
    }
    else {
      this.labelValidator.setIcon(Utils.isUriCorrect(text) ? IMAGE_OK : IMAGE_BAD);
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    labelBrowseCurrentLink = new javax.swing.JLabel();
    textFieldURI = new javax.swing.JTextField();
    labelValidator = new javax.swing.JLabel();
    butonReset = new javax.swing.JButton();

    setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
    setLayout(new java.awt.GridBagLayout());

    labelBrowseCurrentLink.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/url_link.png"))); // NOI18N
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/igormaznitsa/nbmindmap/i18n/Bundle"); // NOI18N
    labelBrowseCurrentLink.setToolTipText(bundle.getString("UriEditPanel.labelBrowseCurrentLink.toolTipText_1")); // NOI18N
    labelBrowseCurrentLink.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    labelBrowseCurrentLink.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    labelBrowseCurrentLink.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        labelBrowseCurrentLinkMouseClicked(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.ipadx = 10;
    add(labelBrowseCurrentLink, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1000.0;
    add(textFieldURI, gridBagConstraints);

    labelValidator.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    labelValidator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/question16.png"))); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.ipadx = 10;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    add(labelValidator, gridBagConstraints);

    butonReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cross16.png"))); // NOI18N
    butonReset.setFocusable(false);
    butonReset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        butonResetActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    add(butonReset, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

  private void labelBrowseCurrentLinkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelBrowseCurrentLinkMouseClicked
    if (evt.getClickCount() > 1) {
      try {
        UiUtils.browseURI(new URI(this.getText().trim()), false);
      }
      catch (URISyntaxException ex) {
        LOGGER.error("Can't start browser for URI syntax error", ex); //NOI18N
        Toolkit.getDefaultToolkit().beep();
      }
    }
  }//GEN-LAST:event_labelBrowseCurrentLinkMouseClicked

  private void butonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butonResetActionPerformed
    this.textFieldURI.setText(""); //NOI18N
  }//GEN-LAST:event_butonResetActionPerformed


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton butonReset;
  private javax.swing.JLabel labelBrowseCurrentLink;
  private javax.swing.JLabel labelValidator;
  private javax.swing.JTextField textFieldURI;
  // End of variables declaration//GEN-END:variables
}
